<template>
    <div class="login-container">
        <div class="login-box">
            <div class="login-header">
                <h2>后台管理系统</h2>
                <p>欢迎登录</p>
            </div>

            <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form"
                @keyup.enter="handleLogin">
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" placeholder="请输入用户名" size="large" prefix-icon="User" />
                </el-form-item>

                <el-form-item prop="password">
                    <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" size="large"
                        prefix-icon="Lock" show-password />
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" size="large" class="login-btn" :loading="loading" @click="handleLogin">
                        登录
                    </el-button>
                </el-form-item>

                <div class="register-link">
                    <el-link type="primary" @click="showRegister = true">
                        还没有账号？点击注册
                    </el-link>
                </div>
            </el-form>
        </div>

        <!-- 注册对话框 -->
        <el-dialog v-model="showRegister" title="注册新账号" width="400px" :before-close="handleCloseRegister">
            <el-form ref="registerFormRef" :model="registerForm" :rules="registerRules" label-width="80px">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="registerForm.username" />
                </el-form-item>

                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="registerForm.email" type="email" />
                </el-form-item>

                <el-form-item label="密码" prop="password">
                    <el-input v-model="registerForm.password" type="password" show-password />
                </el-form-item>

                <el-form-item label="确认密码" prop="confirmPassword">
                    <el-input v-model="registerForm.confirmPassword" type="password" show-password />
                </el-form-item>
            </el-form>

            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="showRegister = false">取消</el-button>
                    <el-button type="primary" :loading="registerLoading" @click="handleRegister">
                        注册
                    </el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { ElMessage } from 'element-plus'

const router = useRouter()
const authStore = useAuthStore()

const loginFormRef = ref()
const registerFormRef = ref()
const loading = ref(false)
const registerLoading = ref(false)
const showRegister = ref(false)

// 登录表单
const loginForm = reactive({
    username: 'admin',
    password: 'admin123'
})

const loginRules = {
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' }
    ]
}

// 注册表单
const registerForm = reactive({
    username: '',
    email: '',
    password: '',
    confirmPassword: ''
})

const registerRules = {
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
    ],
    email: [
        { required: true, message: '请输入邮箱', trigger: 'blur' },
        { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
    ],
    confirmPassword: [
        { required: true, message: '请确认密码', trigger: 'blur' },
        {
            validator: (rule, value, callback) => {
                if (value !== registerForm.password) {
                    callback(new Error('两次输入密码不一致'))
                } else {
                    callback()
                }
            },
            trigger: 'blur'
        }
    ]
}

// 登录处理
const handleLogin = async () => {
    if (!loginFormRef.value) return

    await loginFormRef.value.validate(async (valid) => {
        if (valid) {
            loading.value = true
            try {
                const result = await authStore.login(loginForm)
                if (result.success) {
                    ElMessage.success(result.message)
                    // 添加延迟确保状态更新完成
                    setTimeout(() => {
                        router.push('/')
                    }, 100)
                } else {
                    ElMessage.error(result.message)
                }
            } finally {
                loading.value = false
            }
        }
    })
}

// 注册处理
const handleRegister = async () => {
    if (!registerFormRef.value) return

    await registerFormRef.value.validate(async (valid) => {
        if (valid) {
            registerLoading.value = true
            try {
                const result = await authStore.register(registerForm)
                if (result.success) {
                    ElMessage.success(result.message)
                    showRegister.value = false
                    // 重置表单
                    registerFormRef.value.resetFields()
                } else {
                    ElMessage.error(result.message)
                }
            } finally {
                registerLoading.value = false
            }
        }
    })
}

// 关闭注册对话框
const handleCloseRegister = () => {
    registerFormRef.value?.resetFields()
    showRegister.value = false
}
</script>

<style scoped>
.login-container {
    height: 100vh;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    display: flex;
    justify-content: center;
    align-items: center;
}

.login-box {
    width: 400px;
    padding: 40px;
    background: white;
    border-radius: 10px;
    box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
}

.login-header {
    text-align: center;
    margin-bottom: 30px;
}

.login-header h2 {
    color: #303133;
    margin-bottom: 10px;
}

.login-header p {
    color: #909399;
    margin: 0;
}

.login-form {
    width: 100%;
}

.login-btn {
    width: 100%;
}

.register-link {
    text-align: center;
    margin-top: 20px;
}
</style>